package com.merry.gulimail.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.merry.common.utils.PageUtils;
import com.merry.common.utils.Query;
import com.merry.gulimail.product.dao.CategoryDao;
import com.merry.gulimail.product.entity.CategoryEntity;
import com.merry.gulimail.product.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    //可以使用 ServiceImpl中的baseMapper
    @Resource
    CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> retrieveAllCategory() {
        //获取所有的category
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);

        //找到所有的一级菜单
        List<CategoryEntity> Level1Menus = categoryEntities.stream().filter(
                //一级菜单
                (item) -> {
                    return item.getCatLevel() == 1;
                }
        ).map(
                //找到一级菜单的子菜单setChildrenCategory
                (item) -> {
                    item.setChildrenCategory(getChildren(item, categoryEntities));
                    return item;
                }
        ).sorted(
                (cate1, cate2) -> {
                    return (cate1.getSort()==null?0:cate1.getSort()) - (cate2.getSort()==null?0:cate2.getSort());
                }
        ).collect(Collectors.toList());

        return Level1Menus;
    }


    //递归查找所有的子菜单。
    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter(
                (item) -> {
                    return item.getParentCid() == root.getCatId();
                }
        ).map(
                //1. 找到子菜单
                (item) -> {
                    item.setChildrenCategory(getChildren(item,all));
                    return item;
                }
        ).sorted(
                //2. 菜单排序
                (cate1,cate2) -> {return (cate1.getSort()==null?0:cate1.getSort()) - (cate2.getSort()==null?0:cate2.getSort());}
        ).collect(Collectors.toList());

        return children;
    }

    /**
     * this method will call DAO to delete pms_category by ids
     * and only delete no children data
     * @param idList
     * @return
     */
    @Override
    public int removeByCatIds(List<Long> idList) {
        //TODO 检查当前删除的菜单 是否被别的地方引用
        //need to delete
        List<Long> collect = idList.stream().filter(
                (item) -> {
                    List<CategoryEntity> categoryEntities = categoryDao.selectByParentId(item);
                    //no children
                    return categoryEntities == null || categoryEntities.size() == 0;
                }
        ).collect(Collectors.toList());

        //判断是否为空
        if(collect.size() == 0){
            return 0;
        }
        //逻辑删除 使用mybatis的逻辑删除功能，实际上是执行update操作。
        return baseMapper.deleteBatchIds(collect);
    }
}